// server.js（完整版）
const express = require("express");
const app = express();
const readline = require("readline");
const fs = require("fs");

app.post("/chat-stream", (req, res) => {
  res.setHeader("Content-Type", "text/event-stream");
  res.setHeader("Cache-Control", "no-cache");
  res.flushHeaders();
  const messages = [];
  let count = 0;

  const sendChar = () => {
    const id = `${Date.now()}`;
    count = 0;
    const rl = readline.createInterface({
      input: fs.createReadStream("chat-stream.txt"),
      crlfDelay: Infinity,
    });
    rl.on("line", (line) => {
      // res.write(`${line}\n`);

      messages.push(
        new Promise((resolve) => {
          count++;
          setTimeout(() => {
            res.write(`${line}\n`);
            resolve(true);
          }, count * 10);
        })
      );
    });
    // 监听文件读取结束事件
    rl.on("close", () => {
      Promise.all(messages).then(() => {
        res.end();
      });
    });
  };

  sendChar();

  req.on("close", () => {
    console.log("Client closed connection");
  });
});

app.get("/messages", (req, res) => {
  fs.readFile("./messages.txt", "utf-8", (err, data) => {
    try {
      const jsonData = JSON.parse(data);
      res.json(jsonData);
    } catch (err) {
      res.status(400).send("Invalid JSON");
    }
  });
});

app.get("/rule/group/get/list/1.0", (req, res) => {
  res.json({
    code: 200,
    data: [
      { id: 1, groupName: "支付", creatorId: "", createName: "" },
      { id: 2, groupName: "验收", creatorId: "", createName: "" },
      { id: 3, groupName: "续签", creatorId: "", createName: "" },
    ],
  });
});

app.get("/review/rule/get/list/1.0", (req, res) => {
  res.json({
    code: 200,
    data: [
      {
        id: 1,
        ruleName: "在合同服务期限条款中，确保服务期限规范合理",
        riskLevel: "0",
        riskAttributionId: "0",
        conditionalIdentifier: "1",
        version: "V1",
        ruleGroupId: "3",
        includeRule: "11111111",
        censoredSearchEngine: "0",
        logicRuleList: [{}],
        exampleList: [
          {
            contractContent: "",
            judgmentResult: "",
          },
        ],
        conditionList: [
          {
            conditionInfo: [
              {
                body: "",
                conditionvalue: "",
                logicalSymbol: "",
              },
            ],
          },
        ],
        type: 1,
      },
      {
        id: 2,
        ruleName: "在合同服务期限条款中，确保服务期限规范合理",
        riskLevel: "1",
        riskAttributionId: "1",
        conditionalIdentifier: "0",
        version: "V2",
        ruleGroupId: "2",
        includeRule: "11111111",
        censoredSearchEngine: "1",
        logicRuleList: [{}],
        exampleList: [
          {
            contractContent: "aaa",
            judgmentResult: "sss",
          },
        ],
        conditionList: [
          {
            conditionInfo: [
              {
                body: "迟滞",
                conditionvalue: "",
                logicalSymbol: "不存在",
              },
            ],
          },
        ],
        type: 1,
      },
    ],
  });
});

app.post("/review/rule/get/info/by/:id/1.0", (req, res) => {
  const { id } = req.params;
  res.json({
    code: 200,
    data: {
      id: Number(id),
    },
  });
});

app.post("/risk/attribution/get/list/1.0", (req, res) => {
  res.json({
    code: 200,
    data: [
      {
        id: "0",
        name: "财务风险",
      },
    ],
  });
});

app.get("/review/checklist/get/list/1.0", (req, res) => {
  res.json({
    code: 200,
    data: [
      {
        applicableId: 0,
        applicableName: "采购合同",
        creatorId: 0,
        creatorName: "zyl",
        id: 0,
        reviewChecklistName: "审查清单1",
        reviewRuleDtoList: [
          {
            censoredSearchEngine: "0",
            conditionList: [
              {
                conditionInfo:
                  "json 方式存储 主体(body) 逻辑符号(logicalSymbol) 条件值(conditionvalue)",
              },
            ],
            conditionalIdentifier: "1",
            creatorId: 0,
            creatorName: "",
            exampleList: [
              {
                contractContent: "",
                judgmentResult: "",
              },
            ],
            id: 0,
            includeRule: "",
            logicRuleList: [
              {
                step: "1111",
              },
            ],
            reviseOpinion: "qqqqq",
            riskAttributionId: 0,
            riskAttributionName: "",
            riskLevel: "1",
            ruleGroupId: 0,
            ruleGroupName: "",
            ruleName: "",
            type: "0",
            updateTime: "",
            version: 0,
          },
        ],
      },
    ],
  });
});

app.get("/review/checklist/get/info/by/:id/1.0", (req, res) => {
  const {id} = req;
  res.json({
    code: 200,
    data: 
      {
        applicableId: '0',
        applicableName: "采购合同",
        creatorId: 0,
        creatorName: "zyl",
        id: id,
        reviewChecklistName: "审查清单1",
        reviewRuleDtoList: [
          {
            censoredSearchEngine: "0",
            conditionList: [
              {
                conditionInfo:
                  "json 方式存储 主体(body) 逻辑符号(logicalSymbol) 条件值(conditionvalue)",
              },
            ],
            conditionalIdentifier: "1",
            creatorId: 0,
            creatorName: "",
            exampleList: [
              {
                contractContent: "",
                judgmentResult: "",
              },
            ],
            id: 0,
            includeRule: "",
            logicRuleList: [
              {
                step: "1111",
              },
            ],
            reviseOpinion: "qqqqq",
            riskAttributionId: 0,
            riskAttributionName: "",
            riskLevel: "1",
            ruleGroupId: 0,
            ruleGroupName: "",
            ruleName: "在合同服务期限条款中，确保服务期限规范合理",
            type: "0",
            updateTime: "",
            version: 0,
          },
          
        ],
      },
    
  });
});




app.listen(3000);
